"""
API响应模型
"""
from typing import Optional, Any, List, Generic, TypeVar
from pydantic import BaseModel, Field
from datetime import datetime

T = TypeVar('T')

class BaseResponse(BaseModel, Generic[T]):
    """基础响应模型"""
    success: bool = Field(..., description="操作是否成功")
    message: str = Field(..., description="响应消息")
    data: Optional[T] = Field(default=None, description="响应数据")
    timestamp: datetime = Field(default_factory=datetime.now, description="响应时间")
    
    class Config:
        schema_extra = {
            "example": {
                "success": True,
                "message": "操作成功",
                "data": {},
                "timestamp": "2024-01-01T10:00:00Z"
            }
        }

class ErrorResponse(BaseModel):
    """错误响应模型"""
    success: bool = Field(default=False, description="操作是否成功")
    message: str = Field(..., description="错误消息")
    error_code: Optional[str] = Field(default=None, description="错误代码")
    error_detail: Optional[str] = Field(default=None, description="错误详情")
    timestamp: datetime = Field(default_factory=datetime.now, description="响应时间")
    
    class Config:
        schema_extra = {
            "example": {
                "success": False,
                "message": "操作失败",
                "error_code": "KAFKA_CONNECTION_ERROR",
                "error_detail": "无法连接到Kafka服务器",
                "timestamp": "2024-01-01T10:00:00Z"
            }
        }

class PaginatedResponse(BaseModel, Generic[T]):
    """分页响应模型"""
    success: bool = Field(..., description="操作是否成功")
    message: str = Field(..., description="响应消息")
    data: List[T] = Field(..., description="数据列表")
    total: int = Field(..., description="总数量")
    page: int = Field(..., description="当前页")
    page_size: int = Field(..., description="每页大小")
    timestamp: datetime = Field(default_factory=datetime.now, description="响应时间")
    
    class Config:
        schema_extra = {
            "example": {
                "success": True,
                "message": "查询成功",
                "data": [],
                "total": 100,
                "page": 1,
                "page_size": 10,
                "timestamp": "2024-01-01T10:00:00Z"
            }
        }

class LogEntry(BaseModel):
    """日志条目模型"""
    id: str = Field(..., description="日志ID")
    timestamp: datetime = Field(..., description="日志时间")
    level: str = Field(..., description="日志级别")
    operation: str = Field(..., description="操作类型")
    message: str = Field(..., description="日志消息")
    details: Optional[dict] = Field(default=None, description="详细信息")
    success: bool = Field(..., description="操作是否成功")
    
    class Config:
        schema_extra = {
            "example": {
                "id": "log-12345",
                "timestamp": "2024-01-01T10:00:00Z",
                "level": "INFO",
                "operation": "SEND_MESSAGE",
                "message": "消息发送成功",
                "details": {"topic": "test-topic", "partition": 0},
                "success": True
            }
        } 